package com.hp.attendance.dao;

import com.hp.attendance.entity.Department;
import com.hp.attendance.entity.Position;
import com.hp.attendance.util.BaseDAO;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class PositionDao extends BaseDAO {
    public List<Position> getList() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql = "select * from position";
        List<Position> list = queryList(sql, new Object[0], new Position());
        return list;
    }
    //分页查询记录
    public  List<Position> getLimitList(int pageNo, int pageSize) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select * from position limit ?,?";
        Object[] obj ={(pageNo-1)*pageSize,pageSize};
        List<Position> positions = queryList(sql, obj, new Position());
        return positions;
    }

    //根据id查询对象
    public Position getPosition(int id) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select * from position where id=?";
        Object[] obj = {id};
        Position department = queryOne(sql, obj, new Position());
        return department;
    }
    //查询总记录
    public long getTotal() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select count(1) from position";
        int count = queryCount(sql, new Object[0]);
        return count;
    }

    public void addPosition(Position position) throws SQLException, IOException, ClassNotFoundException {
        String sql ="insert into position values(null,?,?,?,?)";
        Object[] obj={position.getPosition_number(),position.getName(),position.getLevel(),position.getNotes()};
        addOrUpdOrDel(sql,obj);
    }



    /**
     * 修改菜单
     * @param
     */
    public void updatePosition(Position position) throws SQLException, IOException, ClassNotFoundException {
        String sql ="update position set position_number=?,name=?,level=?,notes=? where id=?";
        Object[] obj={position.getPosition_number(),position.getName(),position.getLevel(),position.getNotes(),position.getId()};
        addOrUpdOrDel(sql,obj);
    }

    /**
     * 删除菜单
     * @param id
     */
    public void delPosition(int id) throws SQLException, IOException, ClassNotFoundException {
        String sql ="delete from position where id=?";
        Object[] obj ={id};
        addOrUpdOrDel(sql,obj);
    }

//部门编号最大值
public Integer getMaxDepartmentnumber() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
    String sql ="select MAX(position_number) from position";
    int i = queryCount(sql, new Object[0]);
    return i;
}


}
